<?php 
$con = mysql_connect("localhost","root","");
if(!$con)
{
	echo "Unable to connect with server and check the sever details".mysql_error();
}
mysql_select_db("stms",$con);



/*if ($_FILES["file"]["error"] > 0)
{
  echo "Error: " . $_FILES["file"]["error"] . "<br />";//error code resulting from the file upload
}
else
{
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";//name of the uploaded file
  echo "Type: " . $_FILES["file"]["type"] . "<br />";//type of the uploaded file
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";//size in bytes of the uploaded file
  echo "Stored in: " . $_FILES["file"]["tmp_name"];//name of the temporarycopy of the file stored on the server
}*/
$sub="submit";
$check="CHECK";
$fle=$_FILES["file"]["name"];
//$fle=$_POST['submit'];

if (file_exists("Upload/" . $_FILES["file"]["name"]))
{
    //echo "dgbshbnsdetgj";
	//echo $_FILES["file"]["name"] . " already exists. ";
	header("Location:http://localhost/stms/pages/HometoAdmin.php?err=This file already exists");
}
else
{
    move_uploaded_file($_FILES["file"]["tmp_name"],"Upload/" . $_FILES["file"]["name"]);
	$header = null;
	$data = array(); 
	$csvData = str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
	$delimiter = ",";
	foreach($csvData as $csvLine)
	{ 
		if(is_null($header)) $header = explode($delimiter, $csvLine); 
		else
		{ 
		if("Upload/user.csv")
		{
	
			$sql="INSERT INTO basicdetails VALUES(";
			$items = explode($delimiter, $csvLine); 
			for($n = 0, $m = count($header); $n < $m; $n++)
			{  
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
			} 
			$sql.= ")";
			//echo $sql. "<br />";
			//echo "Your datum successfully saved in database";
			
			mysql_query($sql);
			$fle=$_FILES["file"]["name"];
	//echo $fle;
		header("Location:http://localhost/stms/pages/viewall.php?err=$fle");


			//$_SESSION['user']="Upload/user.csv";

		}
		//echo ('Upload/'.$_FILES["file"]["name"]);
		 if("Upload/al.csv")
		{
			$sql="INSERT INTO gcealdetails VALUES(";
			$items = explode($delimiter, $csvLine); 
			for($n = 0, $m = count($header); $n < $m; $n++)
			{  
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
			} 
			$sql.= ")";
			//echo $sql. "<br />";
			//echo "Your datum successfully saved in database";
			
			mysql_query($sql);
			//$fle=$_FILES["file"]["name"];
				header("Location:http://localhost/stms/pages/viewall.php?err=$fle");
		

		}
		if("Upload/comb.csv")
		{
		
			$sql="INSERT INTO combination VALUES(";
			$items = explode($delimiter, $csvLine); 
			for($n = 0, $m = count($header); $n < $m; $n++)
			{  
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
			} 
			$sql.= ")";
			//echo $sql. "<br />";
			//echo "Your datum successfully saved in database";
			
			mysql_query($sql);
							header("Location:http://localhost/stms/pages/viewall.php?err=$fle");

		}
		 if("Upload/attend.csv")
		{
			$sql="INSERT INTO attendence VALUES(";
			$items = explode($delimiter, $csvLine); 
			for($n = 0, $m = count($header); $n < $m; $n++)
			{  
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
			} 
			$sql.= ")";
			//echo $sql. "<br />";
			//echo "Your datum successfully saved in database";
			
			mysql_query($sql);
					//header("Location:http://localhost/stms/pages/viewalldet.php");
		header("Location:http://localhost/stms/pages/viewall.php?err=$fle");

		}
		
		
		
		 
		
		
		
		if("Upload/result.csv")
		{
			$sql="INSERT INTO results VALUES(";
			$items = explode($delimiter, $csvLine); 
			for($n = 0, $m = count($header); $n < $m; $n++)
			{  
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
			} 
			$sql.= ")";
			//echo $sql. "<br />";
			//echo "Your datum successfully saved in database";
			
			mysql_query($sql);
		header("Location:http://localhost/stms/pages/viewall.php?err=$fle");
		}
		
//header("Location:http://localhost/stms/pages/viewall.php");
		
		
		} 
	}
}
?> 